(function () {
    /**
     * 充值弹窗扩展
     * @class AlertRechargeUI
     * @constructor
     */
    function AlertRechargeUI() {
        AlertRechargeUI.super(this);

        this.price = [];

        this.bindEvent();
        this.rechargeTab.selectedIndex = -1;
    }
    Laya.class(AlertRechargeUI, "ui.Alert.Recharge", ui.Alert.RechargeUI);

    var _proto = AlertRechargeUI.prototype;
    _proto.updateText = function (value) {
        var price = this.price;

        for(var i = 0; i < 4; i++){
            if(value == price[i]){
                this.rechargeTab.selectedIndex = i;
                break;
            }else{
                this.rechargeTab.selectedIndex = -1;
            }
        }
        
        this.rechargeCustom.text = value;
    }
    /**
     * @privite
     * 绑定相关事件
     */
    _proto.bindEvent = function () {
        //获取弹窗的4个价位
        for(var i = 0; i < 4; i++){
            this.price.push(this.rechargeTab.getChildByName("price_" + i).text.replace("元", "") | 0);
        }

        //绑定Tab的点击事件
        this.rechargeTab.selectHandler = new Laya.Handler(this, function (index) {
            this.rechargeCustom.text = this.price[index] || 10000;
        });

        this.rechargeCustom.on(Laya.Event.CLICK, this, function () {
            viewer.publish("custom.input.show", this.rechargeCustom.text);
        });

        //充值按钮
        this.btnRecharge.on(Laya.Event.CLICK, this, function () {
            this.close("recharge");
        });

        //弹窗关闭时的回调函数
        this.closeHandler = new Laya.Handler(this, function (name) {
            var rechargeValue = this.rechargeCustom.text;
            if(name === "recharge"){
                if(!window.Zepto && !window.jQuery){return;}
                
                var $form = $('<form methog="post" action=""></form>');
                $form.append('<input type="text" name="act" value="payment" />')
                    .append('<input type="text" name="gameId" value="' + gameId + '" />')
                    .append('<input type="text" name="tradeName" value="' + tradeName + '" />')
                    .append('<input type="text" name="amount" value="' + rechargeValue + '" />')
                    .append('<input type="text" name="platform" value="' + platform + '" />')
                    .append('<input type="text" name="redirect_uri" value="' + redirect_uri + '" />');
                $("body").append($form);$form.submit();
            }
        });

        viewer.register("custom.input", this.updateText.bind(this));
    }

    /**
     * @public
     * 显示充值弹窗
     */
    _proto.display = function () {
        this.rechargeTab.selectedIndex = 2;

        this.popup();
    }
})();